import os
import time
import pygame
from pygame.locals import *


print "Starting test...\n"

# Init PyGame
pygame.init()

# Print the number of joysticks
print "Joysticks Found: " + str(pygame.joystick.get_count()) + "\n"

# Get a joystick
myjoystick = pygame.joystick.Joystick(0)
myjoystick.init()

# Make button, axis, and hat arrays
axes = [0 for this_axis in range(myjoystick.get_numaxes())]
hats = [0 for this_hat in range(myjoystick.get_numhats())]
buttons = [0 for this_button in range(myjoystick.get_numbuttons())]

# Loop
while True:
    
    # check for events
    for event in pygame.event.get():
        if event.type == QUIT:
            print "Done!\n"
            pygame.quit()
            sys.exit()
        elif event.type == JOYAXISMOTION:
            axes[event.axis] = (event.value)*127
        elif event.type == JOYHATMOTION:
            hats[event.hat] = event.value
        elif event.type == JOYBUTTONUP:
            buttons[event.button] = 0
        elif event.type == JOYBUTTONDOWN:
            buttons[event.button] = 1

    # Clear Screen (Windows)
    time.sleep(0.1)
    os.system("cls")
    
    # Print joystick information
    print "ID: " + str(myjoystick.get_id())
    print "Name: " + str(myjoystick.get_name())
    print "Num Axes: " + str(myjoystick.get_numaxes())
    print "Num Hats: " + str(myjoystick.get_numhats())
    print "Num Buttons: " + str(myjoystick.get_numbuttons())

    # Print Axes
    for this_axis in range(len(axes)):
        print "Axis " + str(this_axis) + ": " + str(axes[this_axis])

    # Print Hats
    for this_hat in range(len(hats)):
        print "Hat " + str(this_hat) + ": " + str(hats[this_hat])
            
    # Print Buttons
    button_string = ''
    button_string += "Buttons: "
    for this_button in range(len(buttons)):
        button_string += str(this_button) + ":" + str(buttons[this_button]) + " "
    print button_string

